﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using V5_Apps.Core;
using WeifenLuo.WinFormsUI.Docking;

namespace V5_Apps.Plugins.动态调用IronPython {
    public partial class frmMain : DockContent, IApp {
        public frmMain() {
            InitializeComponent();
        }

        public ToolStripMenuItem[] GetMenuItem() {
            List<ToolStripMenuItem> list = new List<ToolStripMenuItem>();
            ToolStripMenuItem ts1 = new ToolStripMenuItem() { Text = "工具1" };
            ts1.Click += delegate(object sender, EventArgs e) {
                MessageBox.Show("工具1");
            };
            list.Add(ts1);

            ToolStripMenuItem t2 = new ToolStripMenuItem() { Text = "工具2" };
            t2.Click += delegate(object sender, EventArgs e) {
                MessageBox.Show("工具2");
            };
            list.Add(t2);

            return list.ToArray();
        }


        public ToolStripItem[] GetToolBarItem() {
            throw new NotImplementedException();
        }

        public ToolStripItem[] GetStatusItem() {
            throw new NotImplementedException();
        }

        private void btnRunPython_Click(object sender, EventArgs e) {
            this.txtLogView.Text = string.Empty;

            var result1 = RunPython("", "hello.py");

            this.txtLogView.Text = result1.ToString();
            //MessageBox.Show(result1.ToString());
            return;

            //ScriptEngine engine = Python.CreateEngine();
            //ScriptScope scope = engine.CreateScope();
            //ScriptSource source = engine.CreateScriptSourceFromString(
            //    @"import sys" + "\n" +
            //    @"sys.path.append("".\scripts\pythonlib.zip"")" + "\n" +
            //    @"sys.path.append("".\scripts"")" + "\n"
            //    );
            //var py = source.Execute(scope);

            ScriptEngine engine = Python.CreateEngine();
            ScriptScope scope = engine.CreateScope();
            //var strExpression = "1+2";

            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hello.py");

            var sourceCode = engine.CreateScriptSourceFromFile(fileName);
            sourceCode.Compile();

            sourceCode.Execute();

            var main = scope.GetVariable<Func<string, string>>("main");

            var result = main("34samu");

            MessageBox.Show(result);

        }

        private object RunPython(string pythonCode, string pythonFile) {

            ScriptEngine _engine = Python.CreateEngine();

            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hello.py");


            string pythonLibsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dll\\V5_PythonLibs.dll");
            #region 字符串
            //加载必须的类库
            var code = @"import sys" + "\n" +
                @"from System.IO import Path" + "\n" +
                @"sys.path.append("".\pythonlib.zip"")" + "\n" +
                @"import clr" + "\n" +
                @"clr.AddReferenceToFileAndPath(Path.GetFullPath(r'dll\V5_PythonLibs.dll'))" + "\n" +
                @"execfile('hello.py')";
            var source = _engine.CreateScriptSourceFromString(code);
            //E:\\WWW\\ASPNET\\V5_Demo\\V5_TestIronPythonPython\\V5_TestIronPythonPython\\bin\\Debug\\中文\\V5_PythonLibs.dll
            var scope = _engine.CreateScope();
            source.Execute(scope);
            #endregion

            #region 文件
            //var source = _engine.CreateScriptSourceFromFile(fileName, Encoding.Default, SourceCodeKind.Statements);

            //CompiledCode _code = source.Compile();

            //var scope = _engine.CreateScope();

            //_code.Execute(scope);
            #endregion

            var main = scope.GetVariable<Func<string, string>>("main");

            return main("34samu");

        }

        private void txtLogView_TextChanged(object sender, EventArgs e) {

        }
    }
}
